package com.njupt.DynamicProgramming;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/2/14 14:09
 * @Description: 322. 零钱兑换
 * @Version: 1.0
 */


public class CoinChange_322 {

    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount + 1];
        for (int i = 1; i < dp.length; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        dp[0] = 0;
        for (int i = 0; i < coins.length; i++) {
            for (int j = 0; j <= amount; j++) {
                if(j>=coins[i]
                        && dp[j-coins[i]] != Integer.MAX_VALUE){
                    dp[j] = Math.min(dp[j], dp[j - coins[i]] +1);
                }


            }
        }
        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }

    public static void main(String[] args) {
        CoinChange_322 test = new CoinChange_322();
        int[] coins = {1, 2, 5};
        int result = test.coinChange(coins, 11);
        System.out.println(result);
    }
}
